<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var minimumDeletions = function (nums) {

      let max = Math.max(...nums)
      let min = Math.min(...nums)
      let arr = []
      for (let i = 0; i < nums.length; i++) {
        if (nums[i] == max || nums[i] == min) {
          arr.push(i)
        }
      }
      let a = Math.max(...arr)
      let b = Math.min(...arr)
      let c = nums.length - a + 1 + b
      let e = nums.length - b
      let d = Math.min(a + 1, c, e)
      return d
    };
    console.log(minimumDeletions([2, 10, 7, 5, 4, 1, 8, 6]));
  </script>
</body>

</html>